package com.diyizu.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diyizu.youdianda.common.PageResult;
import com.diyizu.youdianda.common.R;
import com.diyizu.youdianda.entity.YddTag;
import com.diyizu.youdianda.service.YddTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ydd_tag")
public class YddTagController {
    @Autowired
    private YddTagService yddTagService;

    @GetMapping("/all")
    public R all() {
        List<YddTag> listTag = yddTagService.list();
        return R.success(listTag);
    }

    @GetMapping("/list")
    public R list(@RequestParam Map map) {
        String tagname = (String) map.get("tagname");
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");
        Page<YddTag> yddTagPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        LambdaQueryWrapper<YddTag> lqw = new LambdaQueryWrapper<>();
        lqw.like(tagname != "", YddTag::getTagname, tagname);
        yddTagService.page(yddTagPage, lqw);
        PageResult pageResult = new PageResult();
        pageResult.setData(yddTagPage.getRecords());
        pageResult.setCount(yddTagPage.getTotal());
        pageResult.setTotalPages((long) Math.ceil((double) yddTagPage.getTotal() / yddTagPage.getSize()));
        pageResult.setPageSize(yddTagPage.getSize());
        pageResult.setCurrentPage(yddTagPage.getCurrent());
        return R.success(pageResult);
    }
    @GetMapping("/del")
    public R del(int id){
        yddTagService.removeById(id);
        return R.success("删除成功");
    }
    @PostMapping("/add")
    public R add(@RequestBody YddTag yddTag){
        yddTagService.save(yddTag);
        return R.success("添加成功");
    }
    @GetMapping("/rowInfo")
    public R rowInfo(int id){
        YddTag byId = yddTagService.getById(id);
        return R.success(byId);
    }
    @PostMapping("/update")
    public R update(@RequestBody YddTag yddTag){
        yddTagService.updateById(yddTag);
        return R.success("修改成功");
    }
}
